package com.fan_main;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Stack;

import javax.swing.JCheckBox;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

abstract public class MyThread extends Thread {
	String webname = "";     //网站名称
	String website = "";     //网址
	String lasttitle = "";   //最后一次关系的标题
	String temptitle;        //临时标题
	int time = 0;            
	boolean act = false;     
	boolean first = true;
	int temptime = 0;
	
	//保持每次更新添加的记录
	Stack<Record> stack = new Stack<Record>();
	//选择框
	public JCheckBox mycheck = null;
	public MyThread() {
		this.setWebname();
		this.setWebsite();
		
		MyMain.threadmap.put(this.webname, this);
		mycheck = new JCheckBox(this.getWebname());
		mycheck.addItemListener(new ItemListener() {
			
			public void itemStateChanged(ItemEvent arg0) {
				if (mycheck.isSelected()) {
					MyMain.threadmap.get(webname).goon();
				} else {
					MyMain.threadmap.get(webname).pause();
				}
				
			}
		});
		
	}
	public MyThread(final String webname,String website) {
		this.webname=webname;
		this.website=website;
		
		MyMain.threadmap.put(this.webname, this);
		mycheck = new JCheckBox(this.getWebname());
		mycheck.addItemListener(new ItemListener() {
			
			public void itemStateChanged(ItemEvent arg0) {
				if (mycheck.isSelected()) {
					MyMain.threadmap.get(webname).goon();
				} else {
					MyMain.threadmap.get(webname).pause();
				}
				
			}
		});
		
	}
	@Override
	public void run() {
		while(true)
		{
			if(act == true)
			{
			    method(downMethod());
			
			}
		    try {
				Thread.sleep(5*1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void pause()
	{
		act = false;
	}
	public void goon()
	{
		act = true;
	}
	abstract public String downMethod();
	abstract void method(String str); 
	void setWebname(){};
	void setWebsite(){};
    //下载网页
    protected String downHTML(String ENCODE) throws IOException
    {
    	
    	URL url = new URL(
				website);
		HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				connection.getInputStream(), ENCODE));
		String HTML = "";
		String str = "";
		while((str = br.readLine())!=null)
		{
			HTML+=str.trim();
		}
		return HTML;
    } 
    //根据下载的html分析网页
    protected NodeList analysHTML(String HTML,String tagname,String tagattr,String tagattrvalue) throws ParserException
    {
    	
    	Parser parser = Parser.createParser(HTML, "utf-8");
		NodeFilter filter1 = new TagNameFilter(tagname);
		NodeFilter andfilter=filter1;
		NodeFilter filter2 =null;
		if(StringUtils.isNotBlank(tagattr)){
			 filter2 = new HasAttributeFilter(tagattr, tagattrvalue);
			 andfilter = new AndFilter(filter1, filter2);
		}
		NodeList nodes = parser.extractAllNodesThatMatch(andfilter);
		return nodes;
    	
    }
  
    //根据连接分析网页
    protected NodeList analysHTML(String tagname,String tagattr,String tagattrvalue) throws ParserException, MalformedURLException, IOException
    {
    	Parser parser = new Parser(
				(HttpURLConnection) (new URL(
						website))
						.openConnection());
		NodeFilter filter1 = new TagNameFilter(tagname);
		NodeFilter filter2 = new HasAttributeFilter(tagattr, tagattrvalue);
		NodeFilter andfilter = new AndFilter(filter1, filter2);
		NodeList nodes = parser.extractAllNodesThatMatch(andfilter);
		return nodes;
    }
	
    protected int gettimtes(int mode)
    {
    	Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH) ;
    	if(mode == 1)
    	{
    		
			 return  (year * 372 + month * 31 + day);
    		
    	}
    	else
    	{
    		 return 0;
    		
    	}
    	
    }
    
	String getWebname()
	{
		return this.webname;
	}
	
	String getWebsite()
	{
		return this.website;
	}
}
